//
//  CSNetWork.m
//  classroom
//
//  Created by Macx on 16/8/4.
//  Copyright © 2016年 LiuZhuo. All rights reserved.
//

#import "CSNetWork.h"
#import "TFHpple.h"
#import "CSTransBuild.h"
#import "CSTranstrueBuild.h"
@interface CSNetWork()
@property(nonatomic,strong)NSData *htmlData;
@end

@implementation CSNetWork
-(NSMutableDictionary *)netWork:(NSString *)buildName dateName:(NSString *)dateName{
 
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *  ison = [userDefaults objectForKey:@"仅Wifi状态下连网"];
     BOOL ison3G = [userDefaults boolForKey:@"网络状态为3G"];
    DDLogDebug(@"%@",ison);
    DDLogDebug(@"%d",ison3G);
    if ([ison isEqualToString:@"开"]&ison3G) {
        return nil;
    }

    NSRange  range = [buildName rangeOfString:@" "];
    NSString  *   areaNow = [buildName substringFromIndex:range.location+1];

    NSString * buildId = [CSTransBuild translation:areaNow];
    NSString * trueBuildName = [CSTranstrueBuild translation:areaNow];
    NSString *  weekName = nil;
    
    if ( [self isPureInt:[dateName substringWithRange:NSMakeRange(1, 2)]] ) {
     weekName = [dateName substringWithRange:NSMakeRange(1, 2)];
    }else{
    weekName = [dateName substringWithRange:NSMakeRange(1, 1)] ;
    }
    NSMutableArray * strArray = [NSMutableArray array];
   
    
    NSString * urlStr = [NSString stringWithFormat:@"http://60.18.131.131:11180/academic/manager/teachresource/schedule/export_room_schedule_detail.jsp?weeks=%@&buildingid1=%@&buildingname=%@",weekName,buildId,trueBuildName] ;
    DDLogDebug(@"%@",urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
      NSMutableDictionary * dict = [NSMutableDictionary dictionary];

    AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc]init];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        [manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            self.htmlData = responseObject;
            DDLogDebug(@"success-=-=-=");
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                TFHpple *xpathParser = [[TFHpple alloc]initWithHTMLData:_htmlData];
                NSArray * dataArray = [xpathParser searchWithXPathQuery:@"//td"];
                for (TFHppleElement * httpElement in dataArray) {
                    [strArray addObject:httpElement.text];
                }
                if (strArray.count==0) {
                    
                    
                }
                [strArray removeObjectsInRange:NSMakeRange(0, 12)];
                NSMutableArray * nameArr = [NSMutableArray array];
                NSMutableArray * countArr = [NSMutableArray array];
                NSMutableArray * typeArr = [NSMutableArray array];
                NSMutableArray * monArr = [NSMutableArray array];
                NSMutableArray * tuesArr = [NSMutableArray array];
                NSMutableArray * wesArr = [NSMutableArray array];
                NSMutableArray * thuArr = [NSMutableArray array];
                NSMutableArray * friArr = [NSMutableArray array];
                NSMutableArray * satArr = [NSMutableArray array];
                NSMutableArray * sunArr = [NSMutableArray array];
                for (int i = 0; i<strArray.count/10; i++) {
                    NSMutableArray * arr = [NSMutableArray array];
                    [arr removeAllObjects];
                    for (int j= i*(10); j<(i +1)*10; j++) {
                        
                        [arr addObject:strArray[j]];
                    }
                    [nameArr addObject:arr[0]];
                    [countArr addObject:arr[1]];
                    [typeArr addObject:arr[2]];
                    [monArr addObject:arr[3]];
                    [tuesArr addObject:arr[4]];
                    [wesArr addObject:arr[5]];
                    [thuArr addObject:arr[6]];
                    [friArr addObject:arr[7]];
                    [satArr addObject:arr[8]];
                    [sunArr addObject:arr[9]];
                }
                
                [dict setObject:nameArr forKey:@"name"];
                [dict setObject:countArr forKey:@"count"];
                [dict setObject:typeArr forKey:@"type"];
                [dict setObject:monArr forKey:@"1"];
                [dict setObject:tuesArr forKey:@"2"];
                [dict setObject:wesArr forKey:@"3"];
                [dict setObject:thuArr forKey:@"4"];
                [dict setObject:friArr forKey:@"5"];
                [dict setObject:satArr forKey:@"6"];
                [dict setObject:sunArr forKey:@"7"];
                //            return dict;
                
            });
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            DDLogDebug(@"%@",error);
        }];

    
   
        return dict;
    
    
}

// 判断字符串是否为整形数字
- (BOOL)isPureInt:(NSString *)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}
@end
